package com.superteam.zzl.view;


import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.superteam.zzl.activity.R;
import com.superteam.zzl.activity.RecommandActivity.Banner1Activity;
import com.superteam.zzl.activity.databinding.StorefragmentLayoutBinding;
import com.superteam.zzl.cans.ContentURLs;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.superteam.zzl.activity.R;
import com.superteam.zzl.activity.databinding.StorefragmentLayoutBinding;


/**
 * Created by zhang on 2017/1/6.
 * 商城
 */


public class MainStoreFragment extends BaseFragment {
    private StorefragmentLayoutBinding store;
    private WebView webView;
    private ProgressBar progressBar;

    public static MainStoreFragment getInstance(String type) {
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        MainStoreFragment fragment = new MainStoreFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        store = DataBindingUtil.inflate(inflater, R.layout.storefragment_layout, container, false);

//        isPrepared = true;
//        lazyLoad();
//        Log.e("data","0000");
        webView = store.webViewStore;
        webView.loadUrl(ContentURLs.store);
        progressBar = store.pbLoad;
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Intent intent = new Intent(getActivity(), Banner1Activity.class);
                intent.putExtra("Link", url);
                intent.putExtra("Flag", "Store");
                startActivityForResult(intent, 100);
                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                progressBar.setVisibility(View.GONE);

            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                loadData();
            }
        });
        return store.getRoot();
    }

    private void loadData() {
        //progressDialog = new MyProgressDialog(getActivity(),R.style.CustomDialog);
        //progressDialog.show();


        //WebView的设置对象
        WebSettings setting = webView.getSettings();
        //是否支持js
        setting.setJavaScriptEnabled(true);
        //设置自适应
        setting.setUseWideViewPort(true);
        webView.setNetworkAvailable(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);//设置js可以直接打开窗口，如window.open()，默认为false
        webView.getSettings().setJavaScriptEnabled(true);//是否允许执行js，默认为false。设置true时，会提醒可能造成XSS漏洞
        webView.getSettings().setSupportZoom(false);//是否可以缩放，默认true
        webView.getSettings().setBuiltInZoomControls(false);//是否显示缩放按钮，默认false
        webView.getSettings().setUseWideViewPort(true);//设置此属性，可任意比例缩放。大视图模式
        webView.getSettings().setLoadWithOverviewMode(true);//和setUseWideViewPort(true)一起解决网页自适应问题
        webView.getSettings().setAppCacheEnabled(true);//是否使用缓存
        webView.getSettings().setDomStorageEnabled(true);//DOM Storage
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            loadData();
        }
    }

}



